// miniprogram/pages/components/subfood.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    steamed_bun:1,
    subfoods:[],
    selectedCount:[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
    allCount:0
  },
  sub_sb(e){
    var index = e.currentTarget.dataset.index
      console.log(e.currentTarget.dataset.index)
    let count = (this.data.selectedCount[index] - 1)
    count=count < 0 ? 0 : count
   var temp=this.data.selectedCount
    temp[index]=count
    this.setData({
      selectedCount: temp
    })
    this.refreshPrices()
  },
  plus_sb(e){
    var index = e.currentTarget.dataset.index
    console.log(e.currentTarget.dataset.index)
    let count = (this.data.selectedCount[index] + 1)
    let temp = this.data.selectedCount
    temp[index] = count
    this.setData({
      selectedCount: temp
    })
    this.refreshPrices()
  },


  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that =this
      wx.request({
        url: 'https://thtl.vip/jishu/getAllSubFood',
        success:function(res){
            that.setData({
              subfoods:res.data
            })
        }
      })
  },
  refreshPrices: function () {
    var that = this
    let count = 0;
    let i = 0
    for (; i < this.data.subfoods.length; i++) {
      count += ((((this.data.subfoods)[i]).price) * (this.data.selectedCount[i]))
    }
    count = count.toFixed(2)
    this.setData({
      allCount: count
    })
  },
  submit:function(e){
    var app=getApp()
    var that=this
    var sort=""
    let tempsubfoods = this.data.subfoods
    let tempcount = this.data.selectedCount
    let i=0
    for (; i < this.data.subfoods.length; i++) {
      if(tempcount[i]!=0){
          sort+=tempsubfoods[i].id+","+tempcount[i]+";"
      }
    }
    console.log(sort)
    wx.request({
      url: 'https://thtl.vip/jishu/submitSubFoodOrder',
      data:{
        userId: app.globalData.userId,
        selectedSort: sort,
        price:that.data.allCount
      },
      success:function(e){
        that.setData({
          allCount: e.data
        })
      }
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
  
})